import { Provide } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { userLoginDTO } from '../DTO/User';
import { SUser } from '../entity/SUser';
import { IUserOptions } from '../interface';
import { CustomError } from '../middleware/customError';

@Provide()
export class UserService {
  @InjectEntityModel(SUser)
  userModel: Repository<SUser>;

  async getUser(options: IUserOptions) {
    return {
      uid: options.uid,
      username: 'mockedName',
      phone: '12345678901',
      email: 'xxx.xxx@xxx.com',
    };
  }

  async login(user: userLoginDTO) {
    const theUser = await this.userModel.findOne({
      NICKNAME: user.username,
    });
    if (!theUser) {
      throw new CustomError(50001, '用户名或密码错误');
    }

    return theUser.USERID;
  }
}
